<?php
namespace app\home\controller;
use think\Controller;
use think\Db;	
use think\Session;
use think\request;
//充值
class Gateway extends Base{
	
	public function xiapay(){
	 $price=$_GET['amount'];
	 $charge=$_GET['charge'];
	 //充值金额加手续费
	 $toamount=$price+$charge;
	 $url='https://hpay.96225.com/HzsmkB2CPay/makeOrder.do';
      //请求流水号
    	$order=orderNum();
    	//流水号
    	$orderid=date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    	//订单号
    	$orders=date('Ymd') . str_pad(mt_rand(1, 999999), 5, '0', STR_PAD_LEFT);
    	$time=date('Ymd H:i:s');
        //var_dump($time);
    	$str="version=1.0.0&transCode=GWP0001&reqSeq=".$order."&merCode=330100710271&chainNo=004&serialNo=".$orderid."&orderNo=".$orders."&dateTime=".$time."&amount=".$toamount."&goods=保证金&notifyURL=http://test.lcqjgj.com/home/userrecharge/citizen&returnURL=http://test.lcqjgj.com/home/userpersonal/personalCenter&merCustId=&transType=";
    	$pfxpath='http://test.lcqjgj.com/static/home/lc.pfx';
        $pfxpwd='Lczc_164';
        $sign=$this->sign(iconv('UTF-8','GBK',$str),$pfxpath,$pfxpwd);
    	//$data="version=1.0.0&transCode=GWP0001&reqSeq=".$order."&merCode=330100710271&chainNo=004&serialNo=".$orderid."&orderNo=".$orders."&dateTime=".$time."&amount=".$price."&goods=商品&notifyURL=http://test.lcqjgj.com/home/userrecharge/citizen&returnURL=http://test.lcqjgj.com/home/Deal/deal&merCustId=".Session::get('uid')."&transType=PSRC&sign=".$sign;
        $data = 'version=' .urlencode('1.0.0') . '&transCode=' .urlencode('GWP0001') . '&reqSeq=' .urlencode($order) . '&merCode=' .urlencode('330100710271') . '&chainNo=' .urlencode('004') . '&serialNo=' .urlencode($orderid) . '&orderNo=' .urlencode($orders) . '&dateTime=' .($time) . '&amount=' .urlencode($toamount) . '&goods=' .urlencode('保证金') . '&notifyURL=' .('http://test.lcqjgj.com/home/userrecharge/citizen') . '&returnURL=' .('http://test.lcqjgj.com/home/userpersonal/personalCenter') . '&merCustId=&transType=&sign=' .urlencode($sign);
        // var_dump($data);
        //充值表
         $arr=array();
         $arr['uid']=Session::get('uid');
         $arr['tradeNo']=$order;
       //$arr['retradeNo']=$orderid;
       //$arr['platformtradeNo']=$orderid;
         $arr['amount']=$price;
         $arr['paymentCode']=6;
         $arr['offline']=1;
         $arr['paymentstate']=0;
         $arr['poundage']=$charge;
       //$arr['addtime']=$time;
         Db::name('user_recharge')->insert($arr);
       $res=$this->https_request($url,$data);
       $res=str_replace('<link href="','<link href="https://hpay.96225.com/HzsmkB2CPay/',$res);
	   $res=str_replace('src="','src="https://hpay.96225.com/HzsmkB2CPay/',$res);
	   $res=str_replace('action="','action="https://hpay.96225.com/HzsmkB2CPay/',$res);
      // var_dump($res);
		echo $res;
	}
	
	
	
/*//查询银行
public function  cardid(){
	 $price=$_GET['amount'];
	 $url='https://hpay.96225.com/HzsmkB2CPay/merB2CPay.do';
      //请求流水号
    	$order=date('Ymd') . str_pad(mt_rand(1, 99999999), 5, '0', STR_PAD_LEFT);
    	//流水号
    	$orderid=date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    	//订单号
    	$orders=date('Ymd') . str_pad(mt_rand(1, 999999), 5, '0', STR_PAD_LEFT);
    	$time=date('Ymd H:i:s');
    	var_dump($time);
    	$str="version=1.0.0&transCode=GWP0002&reqSeq=".$order."&merCode=330100710271&chainNo=004&amount=".$price;
    	$pfxpath='http://test.lcqjgj.com/static/home/lc.pfx';
        $pfxpwd='Lczc_164';
        $sign=$this->sign(iconv('UTF-8','GBK',$str),$pfxpath,$pfxpwd);
    	//$data="version=1.0.0&transCode=GWP0001&reqSeq=".$order."&merCode=330100710271&chainNo=004&serialNo=".$orderid."&orderNo=".$orders."&dateTime=".$time."&amount=".$price."&goods=商品&notifyURL=http://test.lcqjgj.com/home/userrecharge/citizen&returnURL=http://test.lcqjgj.com/home/Deal/deal&merCustId=".Session::get('uid')."&transType=PSRC&sign=".$sign;
        $data = 'version=' .urlencode('1.0.0') . '&transCode=' .urlencode('GWP0002') . '&reqSeq=' .urlencode($order) . '&merCode=' .urlencode('330100710271') . '&chainNo=' .urlencode('004') .'&amount=' .urlencode($price) .'&sign=' .urlencode($sign);
       var_dump($data);
       $res=$this->https_request($url,$data);
       var_dump($res);
	
}*/

public function sign($data,$pfxpath,$pfxpwd) {
    $certs = array();
    openssl_pkcs12_read(file_get_contents($pfxpath), $certs, $pfxpwd); // 其中password为你的证书密码
    if (!$certs) {
		return;
	}
    $signature = '';  
    openssl_sign($data, $signature, $certs['pkey']);
    return base64_encode($signature); 
}

function https_request($url,$data = null)
{
    // php curl 发起get或者post请求
    // curl 初始化
    $curl = curl_init();	// curl 设置
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

    // 判断 $data get  or post
    if ( !empty($data) ) {
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    //curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

    // 执行
    $res = curl_exec($curl);
    curl_close($curl);
    return $res;

}
}

?>